package com.platform.common.rabbitmq.service;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@SuppressWarnings(value = { "unchecked", "rawtypes" })
@Component
public class RMqService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(Object msg, String queueEnum) throws Exception{
        this.sendMq(msg,queueEnum);
    }

    public void sendEx(Object msg,String exchange, String queueEnum) throws Exception{
        this.sendMqEx(exchange,queueEnum,msg);
    }

    /***
     * mq发送信息
     * @param msg
     * @param queueEnum
     * @throws Exception
     */
    public void sendMq(Object msg , String queueEnum) throws Exception{
        rabbitTemplate.convertAndSend(queueEnum,msg);
    }

    /***
     * 包含交换机
     * @param exchange
     * @param routingKey
     * @param msg
     * @throws Exception
     */
    public void sendMqEx(String exchange,  String routingKey,Object msg ) throws Exception{
        rabbitTemplate.convertAndSend(exchange,routingKey,msg);
    }

    /***
     * 接收消息
     * @param queue
     * @param timeoutMillis
     * @return
     */
    public Message receive(String queue,Long timeoutMillis){
        return rabbitTemplate.receive(queue,timeoutMillis);
    }


}
